{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Basics of Python: Lists </h2>\n",
    "\n",
    "We review using Lists in python here. \n",
    "\n",
    "Please run each cell and check the results.\n",
    "\n",
    "A list (or array) is a collection of objects (variables) separated by comma. \n",
    "\n",
    "The order is important, and we can access each element in the list with its index starting from 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# here is a list holding all even numbers between 10 and 20\n",
    "L = [10, 12, 14, 16, 18, 20]\n",
    "\n",
    "# let's print the list\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's print each element by using its index but in reverse order\n",
    "print(L[5],L[4],L[3],L[2],L[1],L[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's print the length (size) of list\n",
    "print(len(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's print each element and its index in the list\n",
    "# we use a for-loop and the number of iteration is determined by the length of the list\n",
    "# everthing is automatic :-)\n",
    "\n",
    "L = [10, 12, 14, 16, 18, 20]\n",
    "\n",
    "for i in range(len(L)):\n",
    "    print(L[i],\"is the element in our list with the index\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's replace each number in the list with its double value\n",
    "\n",
    "L = [10, 12, 14, 16, 18, 20]\n",
    "\n",
    "# let's print the list before doubling\n",
    "print(\"the list before doubling\",L)\n",
    "\n",
    "for i in range(len(L)):\n",
    "    current_element=L[i] # get the value of the i-th element\n",
    "    L[i] = 2 * current_element # update the value of the i-th element\n",
    "    # let's shorten the replacing code\n",
    "    #L[i] = 2 * L[i]\n",
    "    # or\n",
    "    #L[i] *= 2\n",
    "    \n",
    "# let's print the list after doubling\n",
    "print(\"the list after doubling\",L)\n",
    "\n",
    "# after each exceution of this cell, the latest values will be doubled\n",
    "#     so the values in the list will be exponentially increased "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's define two lists\n",
    "L1 = [1,2,3,4]\n",
    "L2 = [-5,-6,-7,-8]\n",
    "\n",
    "# two lists can be concatenated \n",
    "# the results is a new list\n",
    "\n",
    "print(\"the concatenation of L1 and L2 is\",L1+L2)\n",
    "\n",
    "# the order of terms is important\n",
    "print(\"the concatenation of L2 and L1 is\",L2+L1) # this is a different list than L1+L2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can add a new element to a list, which increases its length/size by 1\n",
    "\n",
    "L = [10, 12, 14, 16, 18, 20]\n",
    "\n",
    "print(L,\"the current length is\",len(L))\n",
    "\n",
    "# we add two values by showing two different methods\n",
    "\n",
    "# L.append(value) directly adds the value as a new element to the list\n",
    "\n",
    "L.append(-4)\n",
    "\n",
    "# we can also use concatenation operator +\n",
    "\n",
    "L = L + [-8] # here [-8] is a list with single element\n",
    "\n",
    "print(L,\"the new length is\",len(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# a list can be multiplied with an integer\n",
    "L = [1,2]\n",
    "\n",
    "# we can consider the multiplication of L by an integer \n",
    "#     as a repeated summation (concatenation) of L by itself\n",
    "# L * 1 is the list itself\n",
    "# L * 2 is L + L (concatenation of L with itself)\n",
    "# L * 3 is L + L + L (concatenation of L with itself twice)\n",
    "# L * m is L + ... + L (concatenation of L with itself m-1 times)\n",
    "# L * 0 is the empty list\n",
    "\n",
    "# L * i is the same as i * L\n",
    "\n",
    "# let's print the different cases\n",
    "for i in range(6):\n",
    "    print(i,\"* L is\",i*L)\n",
    "\n",
    "# this can be useful when initializing a list with the same value(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's create a list of prime numbers less than 100\n",
    "\n",
    "# here is a function determining whether a given number is prime or not\n",
    "def prime(number):\n",
    "    if number < 2: return False\n",
    "    if number == 2: return True \n",
    "    if number % 2 == 0: return False\n",
    "    for i in range(3,number,2):\n",
    "        if number % i == 0: return False \n",
    "    return True\n",
    "# end of function\n",
    "\n",
    "# let's start with an empty list\n",
    "L=[] \n",
    "# what can the length of this list be?\n",
    "print(\"my initial length is\",len(L))\n",
    "\n",
    "for i in range(2,100): \n",
    "    if prime(i):\n",
    "        L.append(i)\n",
    "        # alternative methods:\n",
    "        #L = L + [i]\n",
    "        #L += [i]\n",
    "\n",
    "# print the final list\n",
    "print(L)\n",
    "print(\"my final length is\",len(L))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a given integer $n \\geq 0$, $ S(0) = 0 $, $ S(1)=1 $, and $ S(n) = 1 + 2 + \\cdots + n $.\n",
    "\n",
    "We define list $ L(n) $ such that the element with index $n$ holds $ S(n) $.\n",
    "\n",
    "In other words, the elements of $ L(n) $ is $ [ S(0)~~S(1)~~S(2)~~\\cdots~~S(n) ] $.\n",
    "\n",
    "Let's build the list $ L(20) $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's define the list with S(0) \n",
    "L = [0]\n",
    "\n",
    "# let's iteratively define n and S\n",
    "\n",
    "# initial values\n",
    "n = 0\n",
    "S = 0\n",
    "\n",
    "# the number of iteration\n",
    "N = 20 \n",
    "\n",
    "while n <= N: # we iterate all values from 1 to 20\n",
    "    n = n + 1\n",
    "    S = S + n\n",
    "    L.append(S)\n",
    "    \n",
    "# print the final list\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Fibonacci sequence starts with $ 1 $ and $ 1 $. Then, each next element is equal to summation of the previous two elements:\n",
    "$$\n",
    "    1, 1, 2 , 3 , 5, 8, 13, 21, 34, 55 \\ldots \n",
    "$$\n",
    "\n",
    "Find the first 30 elements of the Fibonacci sequence, store them in a list, and then print the list. \n",
    "\n",
    "You can verify the first 10 elements of your result with the above list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n",
    "\n",
    "F = [1,1]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B14_Python_Basics_Lists_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Lists of different objects </h3>\n",
    "\n",
    "A list can have any type of values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the following list stores certain information about Asja\n",
    "#   name, surname, age, profession, height, weight, partner(s) if any, kid(s) if any, date\n",
    "ASJA = ['Asja','Sarkane',34,'musician',180,65.5,[],['Eleni','Fyodor'],\"October 24, 2018\"]\n",
    "\n",
    "print(ASJA)\n",
    "\n",
    "# Remark that an element of a list can be another list as well."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "Define a list $ N $ with 11 elements such that $ N[i] $ is another list with four elements such that $ [i, i^2, i^3, i^2+i^3] $.\n",
    "\n",
    "The index $ i $ should be form $ 0 $ and $ 10 $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B14_Python_Basics_Lists_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Dictionaries </h3>\n",
    "\n",
    "The outcomes of a quantum program will be stored in a dictionary.\n",
    "\n",
    "Therefore, we very shortly mention about the dictionary data type. \n",
    "\n",
    "A dictionary is a set of paired elements.\n",
    "\n",
    "Each pair is composed by a key and its value, and any value can be accessed by its key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's define a dictionary pairing a person with her/his age\n",
    "\n",
    "ages = {\n",
    "    'Asja':32,\n",
    "    'Balvis':28,\n",
    "    'Fyodor':43\n",
    "}\n",
    "\n",
    "# let print all keys\n",
    "for person in ages:\n",
    "    print(person)\n",
    "    \n",
    "# let's print the values\n",
    "for person in ages:\n",
    "    print(ages[person])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
